<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/16 0016
 * Time: 15:53
 */

//判断是否是手机
function is_mobile(){
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $is_pc = (strpos($agent, 'windows nt')) ? true : false;
    $is_mac = (strpos($agent, 'mac os')) ? true : false;
    $is_iphone = (strpos($agent, 'iphone')) ? true : false;
    $is_android = (strpos($agent, 'android')) ? true : false;
    $is_ipad = (strpos($agent, 'ipad')) ? true : false;

    if($is_pc){
        return  false;
    }

    if($is_mac){
        return  true;
    }

    if($is_iphone){
        return  true;
    }

    if($is_android){
        return  true;
    }

    if($is_ipad){
        return  true;
    }
}//end fun

//获取子代id
function get_idchild($id){
    $cate = M('cate')->select();
    $arr=array();
    foreach($cate as $v){
        if($v['pid']==$id){
            $arr[]=$v['id'];
        }
    }
    return $arr;
}

// 处理字符串，多余的隐藏
function msubstr($string, $length = 0, $ellipsis = '…', $start = 0) {
    $string = strip_tags($string);
    $string = preg_replace('/\n/is', '', $string);
    //$string=preg_replace('/ |　/is','',$string);//清除字符串中的空格
    $string = preg_replace('/&nbsp;/is', '', $string);
    preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $string);
    if (is_array($string) && !empty($string[0])) {
        $string = implode('', $string[0]);
        if (strlen($string) < $start + 1) {
            return '';
        }
        preg_match_all("/./su", $string, $ar);
        $string2 = '';
        $tstr = '';
        for ($i = 0; isset($ar[0][$i]); $i++) {
            if (strlen($tstr) < $start) {
                $tstr .= $ar[0][$i];
            } else {
                if (strlen($string2) < $length + strlen($ar[0][$i])) {
                    $string2 .= $ar[0][$i];
                } else {
                    break;
                }
            }
        }
        return $string == $string2 ? $string2 : $string2 . $ellipsis;
    } else {
        $string = '';
    }
    return $string;
}

//获取子分类ID
function getChlidren($chlidrenId,$isClear=false)
{
    $cate_all = M("cate")->select();//所有分类
    static $rel=array();
    if($isClear)
        $rel=array();
    foreach($cate_all as $k =>$v)
    {
        if($v['pid']==$chlidrenId)
        {
            $rel[]=$v['id'];
            getChlidren($v['id'],false);
        }
    }
    return $rel;
}

//有子类id获取父类ID
function getParent($parentId,$isClear=false)
{
    $cate_all = M("cate")->select();//所有分类
    static $rel=array();
    if($isClear===true)
        $rel=array();
    foreach ($cate_all as $key=>$value)
    {
        if($value['id']==$parentId)
        {
            $rel[]=$value['id'];
            getParent($value['pid'],$isClear);
        }

    }
    return $rel;
}

//根据id获取category
function getunit($id){
    $category = M("cate")->where("id = {$id}")->getField('title');
    return $category;
}

/**
 * 将返回的数据集转换成树
 * @param  array   $list  数据集
 * @param  string  $pk    主键
 * @param  string  $pid   父节点名称
 * @param  string  $child 子节点名称
 * @param  integer $root  根节点ID
 * @return array          转换后的树
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {
    $tree = array();
    // 创建Tree
    if (is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            //将数据的id转换为它的键
            $refer[$data[$pk]] = &$list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            //查询顶级分类
            if ($root == $parentId) {
                $tree[$data[$pk]] = &$list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][] = &$list[$key];
                    //dump($list[$key]);die;
                }
            }
        }
    }
    return $tree;
}//end fun